package com.atwisdom.star.core.dsl.functionClass.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.enums.FirstTypeEnum;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.RedisKeyUtil;
import com.atwisdom.star.common.vo.PageInfo;
import com.atwisdom.star.core.dao.pojo.functionClass.DatabaseBackup;
import com.atwisdom.star.core.dao.pojo.functionClass.DatabaseBackup;
import com.atwisdom.star.core.dao.service.functionClass.DatabaseBackupService;
import com.atwisdom.star.core.dsl.functionClass.DatabaseDslService;
import com.atwisdom.star.core.dsl.modelClass.utils.QueryBuilder;
import com.atwisdom.star.core.dsl.util.DslServiceUtil;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.redis.util.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class DatabaseDslServiceImpl implements DatabaseDslService {
    private DatabaseBackupService databaseBackupService;
    private QueryBuilder queryBuilder;
    private RedisUtil redisUtil;



    /**
     * 分页查询，去除复杂字段
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public PageInfo queryByPage(int pageNumber, int pageSize, String condition, String orderBy) {
        Page<DatabaseBackup> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<DatabaseBackup> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        return getPageInfoResult(pageInfo, queryWrapper);
    }

    public PageInfo getPageInfoResult(Page<DatabaseBackup> pageInfo, QueryWrapper<DatabaseBackup> queryWrapper) {
        Page<DatabaseBackup> queryPage = databaseBackupService.page(pageInfo, queryWrapper);
        List<DatabaseBackup> records = queryPage.getRecords();
        Page<DatabaseBackup> queryPageDTO = new Page<>();
        BeanUtils.copyProperties(queryPage, queryPageDTO);
        queryPageDTO.setRecords(records);
        PageInfo pageResult = PageInfo.of(queryPageDTO);
        return pageResult;
    }

}
